<?php
namespace app\scm\controller;
use app\admin\common\Base;
use think\Db;
use think\Session;

use app\scm\model\OrderInvup as OrderinvupModel;

class Orderinvup extends Base
{
    public function index()
    {
    	$data=$this->request->param();

    	if (isset($data['ajax']) && $data['ajax'] == 100) {
    		$map='';
    		$page=isset($data['page'])?$data['page']:1;
            $page=intval($page);
            $limit=isset($data['rows'])?$data['rows']:10;
            $limit=intval($limit);
            $start=$limit*($page-1);

    		if($map==''){
                $list =Db::name('order_invpu')->where('is_del',0)->where('enterprise_id',$this->EnterpriseId)->order('id asc')->limit($start,$limit)->select();
                $count =Db::name('order_invpu')->where('enterprise_id',$this->EnterpriseId)->where('is_del',0)->count();
            }else{
                $list =Db::name('order_invpu')->where('is_del',0)->order('id asc')->where('enterprise_id',$this->EnterpriseId)->limit($start,$limit)->select();
                $count =Db::name('order_invpu')->where('enterprise_id',$this->EnterpriseId)->where('is_del',0)->count();
            }

            foreach ($list as $key => &$value) {
                
            }

            return $this->grid_data($list,$count,$page,$limit); 
    	}
    	return $this->fetch();
    }

    public function add()
    {
        $supplier = Db::name('supplier')->where('is_del', 0)->select();
        $this->assign('supplier', $supplier);
        return $this->fetch();
    }

    //编辑栏目1-1
    public function edit($id)
    {
        $info = Db::name('supplier')->where('id', $id)->find();
        $info['addtime'] = date('Y-m-d', $info['addtime']);
        
        $supplier = Db::name('supplier')->where('is_del', 0)->select();
        $this->assign('supplier', $supplier);
        $this->assign('info', $info);
        return $this->fetch();
    }
    //编辑栏目1-2
    public function save()
    {
        $status = 0;
        $msg = "操作失败";
        $data = $this->request->param();
        $basedata = $data['basedata'];
        $extdata = $data['extdata'];
        $model = new OrderinvupModel();
        // 启动事务
        Db::startTrans();
        if (empty($basedata['id'])) {
            $basedata['enterprise_id'] = $this->EnterpriseId;
            $basedata['addtime'] = strtotime($basedata['addtime']);
            $basedata['delivery_time'] = strtotime($basedata['delivery_time']);
            $result = $model->allowField(true)->save($basedata);
            if(!$result){
                // 回滚事务
                Db::rollback();
                return ['status' => $status, 'msg' => $msg];
            }
            $lastid = Db::name('order_invpu')->where(['enterprise_id' => $this->EnterpriseId, 'is_del' => 0])->getLastInsID();
            // 新增客户扩展信息
            foreach ($extdata as $key => &$value) {
                if(!empty($value['goods_id'])){

                    $value['order_invpu_id'] = $lastid;
                    $value['enterprise_id'] = $this->EnterpriseId;
                    
                }
            }

            if(!empty($extdata)){
                $sub = Db::name('order_invpu_goods')->insertAll($extdata);
                if(!$sub){
                    // 回滚事务
                    Db::rollback();
                    return ['status' => $status, 'msg' => $msg];
                }
            }

            // 提交事务
            Db::commit();
            $status = 1;
            $msg = "添加成功";
        } else {
            $id = $basedata['id'];
            $basedata['addtime'] = strtotime($basedata['addtime']);
            $basedata['delivery_time'] = strtotime($basedata['delivery_time']);
            $result = $model->allowField(true)->save($basedata,["id"=>$id]);
            if($result === false){
                // 回滚事务
                Db::rollback();
                return ['status' => $status, 'msg' => $msg];
            }
            // 删除所有扩展信息
            $ext_info = Db::name('order_invpu_goods')->where(['order_invpu_id'=>$id, 'is_del'=>0])->count();
            if($ext_info > 0){
                $del_ext = Db::name('order_invpu_goods')->where('order_invpu_id', $id)->update(["is_del"=>1]);
                if($del_ext === false){
                    // 回滚事务
                    Db::rollback();
                    return ['status' => $status, 'msg' => $msg];
                }
            }
            // 新增客户扩展信息
            foreach ($extdata as $key => &$value) {
                if(!empty($value['goods_id'])){

                    $value['order_invpu_id'] = $id;
                    $value['enterprise_id'] = $this->EnterpriseId;
                }
            }

            if(!empty($extdata)){
                $sub = Db::name('order_invpu_goods')->insertAll($extdata);
                if(!$sub){
                    // 回滚事务
                    Db::rollback();
                    return ['status' => $status, 'msg' => $msg];
                }
            }

            // 提交事务
            Db::commit();
            $status = 1;
            $msg = "更新成功";
        }
        return ['status' => $status, 'msg' => $msg];
    }

    // 获取用户扩展信息
    public function getExtInfo($id)
    {
        $ext_info = Db::name('order_invpu_goods')->where(['order_invpu_id'=>$id,'is_del'=>0])->select();
        return json($ext_info);
    }

    //删除栏目1-1
    public function del($id)
    {
        $status = 0;
        $msg = "操作失败";
        $del = Db::name('order_invpu')->where('id', $id)->update(["is_del"=>1]);
        if ($del) {
            // 删除客户扩展信息
            Db::name('order_invpu_goods')->where('order_invpu_id', $id)->update(["is_del"=>1]);

            $status = 1;
            $msg = "删除成功";
        }
        return ['status' => $status, 'msg' => $msg];
    }

    // 状态
    public function status()
    {
        $status = 0;
        $msg = "操作失败";
        $data = $this->request->param();
        $model = new OrderinvupModel();
        $id = $data['id'];
        $result = $model->allowField(true)->save($data,["id"=>$id]);
        if ($result) {
            $status = 1;
            $msg = "更新成功";
        }
        return ['status' => $status, 'msg' => $msg];
    }


}
